package jw3.c10.controllner;

import jw3.c10.model.Userinfo;
import jw3.c10.utils.DBConnection;
import jw3.c10.utils.QueryByClass;
import jw3.c10.view.UserM;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;

public class UserinfoController {

public static  boolean login (String Account,String pwd){
    //登录  控制器

 try {
     QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
     String sql="select * from userinfo where Account=? and Pwd=?";
     List<Userinfo> list =qc.select(Userinfo.class,sql,Account,pwd);

     return  list.size()>0;
 }catch (Exception e){
     e.printStackTrace();
 }
    return true;

}

    public static Vector<Vector<Object>> getAll(){
        String sql="select * from userinfo";
        //1、构造Vector类型用于赋值给表格
        Vector<Vector<Object>> data=new Vector<Vector<Object>>();
        try {
            Connection conn= DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                //创造对象，每次放一条数据
//                UserInfo ui=new UserInfo();
                Vector<Object> ui=new Vector<Object>();

                //将每个字段放入对象
                ui.add(rs.getInt("UserID"));
                ui.add(rs.getString("UserName"));
                ui.add(rs.getString("Account"));
                ui.add(rs.getString("Pwd"));
                ui.add(rs.getInt("Jueisdiction"));

                //每次将一条数据加入到集合
                data.add(ui);
            }
            DBConnection.close(rs,ps,conn);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public   void    pan( ){
        QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
        String sql="SELECT * FROM userinfo where Jueisdiction=?";
    }
    public String login(String Account, String pwd, int Jueisdiction){
        QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
        String sql="select * from userinfo where Account=? and pwd=? and Jueisdiction=?";
        List<Userinfo> list=qc.select(Userinfo.class,sql,Account,pwd,Jueisdiction);
        return list.size()>0?list.get(0).getAccount():"";
    }

    public static int getIDByUser(String user){//根据账号找到id
        String sql="select UserID from userinfo where Account=?";
        QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
        return qc.select(Userinfo.class,sql,user).get(0).getUserID();
    }


    public  static boolean forget(String Account,String UserName){
    try {
        QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
        String sql="select * from userinfo where Account=? and UserName=?";
        List<Userinfo> list =qc.select(Userinfo.class,sql,Account,UserName);
        for (Userinfo item: list) {
            JOptionPane.showMessageDialog(null,"账号"+item.getAccount()+"名字"+item.getUserName()+"密码"+item.getPwd());
        }
        return  list.size()>0;
    }catch (Exception e){
        e.printStackTrace();
    }
    return false;

}
public static int glypd(String a){
    QueryByClass<Userinfo> qc=new QueryByClass<Userinfo>();
    String sql="SELECT Jueisdiction FROM userinfo where Account=?";
    List<Userinfo> userinfoList=qc.select(Userinfo.class,sql,a);
    if (userinfoList.size()>0){
        return userinfoList.get(0).getJueisdiction();
    }
    return 1;
}
    //改: 忘记密码
    public static boolean edit(Userinfo ui){
        String sql="update userinfo set UserName=?,Account=?,pwd=?,Jueisdiction=? where UserID=?";

        return DBConnection.zsg(sql,ui.getUserName(),ui.getAccount(),ui.getPwd(),ui.getJueisdiction()
                , UserM.a);
    }

}
